package LCR_code;


import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author blh
 * @date 2024年07月29日 19:18
 */
public class mianshi0205 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Deque<Integer> l1Deque = new ArrayDeque<>();
        Deque<Integer> l2Deque = new ArrayDeque<>();
        while (l1 != null) {
            l1Deque.push(l1.val);
            l1 = l1.next;
        }
        while (l2 != null) {
            l2Deque.push(l2.val);
            l2 = l2.next;
        }
        StringBuilder l1Sb = new StringBuilder();
        while (!l1Deque.isEmpty()) {
            l1Sb.append(l1Deque.pop());
        }
        StringBuilder l2Sb = new StringBuilder();
        while (!l2Deque.isEmpty()) {
            l2Sb.append(l2Deque.pop());
        }


        while (l2 != null) {
            l2Deque.push(l2.val);
            l2 = l2.next;
        }

        BigInteger bigL1 = new BigInteger(l1Sb.toString());
        BigInteger bigL2 = new BigInteger(l2Sb.toString());
        BigInteger res = bigL2.add(bigL1);
        String resStr = res.toString();
        ListNode dummyHead = new ListNode(-1);
        ListNode pre = null;
        dummyHead.next = pre;
        for (char c : resStr.toCharArray()) {
            int temp = (int)c - 48;
            ListNode tempNode = new ListNode(temp);
            dummyHead.next = tempNode;
            tempNode.next = pre;
            pre = tempNode;
        }
        return dummyHead.next;
    }
}
